<!--
  * 系统设置 列表
  *
  * @Author:    admin
  * @Date:      2022-06-08 21:50:41
   
  
  * @Copyright  jiaoyuyun.org.cn
-->
<template>
  <div>
    <a-form class="smart-query-form">
      <a-row class="smart-query-form-row">
        <a-form-item label="商品名称" class="smart-query-form-item">
          <a-input style="width: 300px" v-model:value="queryForm.spName" placeholder="请输入商品名称" />
        </a-form-item>
          <a-form-item label="商品类型" class="smart-query-form-item">
          <a-input style="width: 300px" v-model:value="queryForm.spType" placeholder="请输入商品类型" />
        </a-form-item>
        <a-form-item class="smart-query-form-item smart-margin-left10">
          <!-- v-privilege="'support:sp:query'" -->
          <a-button type="primary" @click="onSearch" v-privilege="'support:sp:query'">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <!-- v-privilege="'support:sp:query'" -->
          <a-button @click="resetQuery" v-privilege="'support:sp:query'" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
<!-- v-privilege="'support:sp:add'" -->
          <a-button @click="toEditOrAdd()" type="primary" v-privilege="'support:sp:add'" class="smart-margin-left20">
            <template #icon>
              <PlusOutlined />
            </template>
            新建
          </a-button>
        </a-form-item>
      </a-row>
    </a-form>

    <a-card size="small" :bordered="true" :hoverable="true">
      <a-row justify="end">
        <TableOperator class="smart-margin-bottom5" v-model="columns" :tableId="TABLE_ID_CONST.SUPPORT.SP" :refresh="ajaxQuery" />
      </a-row>

      <a-table size="small" :loading="tableLoading" bordered :dataSource="tableData" :columns="columns" rowKey="spId" :pagination="false" >
        <template #bodyCell="{ column, text, record, index }">

          <!-- 当前列是序号列时，显示序号 -->
          <span v-if="column.dataIndex === 'serialNumber'">{{ index + 1 }}</span>
          <!-- 其他列正常显示 -->
          <span v-else>{{ text }}</span>

          <template v-if="column.dataIndex === 'action'">
            <div class="smart-table-operate">
              <a-button @click="toEditOrAdd(record)" v-privilege="'support:sp:update'" type="link">编辑</a-button>
              <a-button @click="deleteSps(record)" v-privilege="'support:sp:update'" danger type="link">删除</a-button>
            </div>
          </template>
        </template>
      </a-table>

      <div class="smart-query-table-page">
        <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="ajaxQuery"
          @showSizeChange="ajaxQuery"
          :show-total="(total) => `共${total}条`"
        />
      </div>
    </a-card>
    <FormModal ref="config" @reloadList="resetQuery" />
  </div>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { spApi } from '/@/api/support/sp-api';
  import FormModal from './sp-edit.vue';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { smartSentry } from '/@/lib/smart-sentry';
  import TableOperator from '/@/components/support/table-operator/index.vue';
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';

  const columns = ref([
    {
          title: '序号',
          dataIndex: 'serialNumber', // 这个dataIndex仅作为标识，不对应实际数据字段
          width: '80px', // 可以自定义宽度
          scopedSlots: { customRender: 'bodyCell' }, // 对应模板中的具名插槽
          align:"center"
    },
    // {
    //   title: '商品编号',
    //   dataIndex: 'spId',
    //   align:"center",
    // },
    {
      title: '商品名称',
      dataIndex: 'spName',
      ellipsis: true,
      align:"center",

    },
    {
      title: '商户编号',
      dataIndex: 'shopId',
      ellipsis: true,
      align:"center",

    },
    {
      title: '商品类型',
      dataIndex: 'spTypeName',
      ellipsis: true,
      align:"center",
    },
     {
      title: '商品价格',
      dataIndex: 'spPrice',
      ellipsis: true,
      align:"center",
    },
     {
      title: '商品图片',
      dataIndex: 'spPic',
      align:"center",
      // colSpan:0,
      className:'tableHiddle'
    },
    {
      title: '备注',
      dataIndex: 'reMark',
      ellipsis: true,
      width: 180,
      align:"center",
    },     
    {
      title: '是否删除',
      dataIndex: 'isDel',
      align:"center",
      className:'tableHiddle'

    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 180,
      align:"center",
    },
     {
      title: '创建者',
      dataIndex: 'createBy',
      ellipsis: true,
      align:"center",
    },
    {
      title: '修改时间',
      dataIndex: 'updateTime',
      width: 120,
      align:"center",
    },
 {
      title: '更新者',
      dataIndex: 'updateBy',
      align:"center",

    },
    {
      title: '操作',
      dataIndex: 'action',
      align:"center",
      fixed: 'right',
    },
  ]);

  // ---------------- 查询数据 -----------------------

  const queryFormState = {
    spId: undefined,
    spName: '',
    spType: '',
    pageNum: 1,
    pageSize: 10,
  };
  const queryForm = reactive({ ...queryFormState });

  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
  }

  function onSearch() {
    queryForm.pageNum = 1;
    ajaxQuery();
  }

  async function ajaxQuery() {
    try {
      tableLoading.value = true;
      //调用查询方法
      let responseModel = await spApi.queryList(queryForm);
      const list = responseModel.data.list;
      console.info('list='+JSON.stringify(list))
      total.value = responseModel.data.total;
      tableData.value = list;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  // ------------------------- 表单操作 弹窗 ------------------------------

  const config = ref();
  function toEditOrAdd(rowData) {
    config.value.showModal(rowData);
  }
  function deleteSps(spData) {
    Modal.confirm({
      title: '提示',
      content: '确定要删除【' + spData.spName + '】吗?',
      okText: '删除',
      okType: 'danger',
      onOk() {
        singleDelete(spData);
      },
      cancelText: '取消',
      onCancel() {},
    });
  }

  async function singleDelete(spData) {
    try {
      SmartLoading.show();
      await spApi.deleteSp(spData);
      message.success('删除成功');
      queryData();
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      SmartLoading.hide();
      resetQuery();
    }
  }

  onMounted(ajaxQuery);
</script>
<style>
.tableHiddle {
  display: none;
}
.tableShow{
  display: revert;
}
</style>